<?php

/**
 * This is the model class for table "{{ad}}".
 *
 * The followings are the available columns in table '{{ad}}':
 * @property integer $id
 * @property integer $uid
 * @property integer $status
 * @property string $date
 * @property integer $clicks
 * @property integer $uv
 * @property integer $budget
 * @property string $details
 *
 * The followings are the available model relations:
 * @property User $u
 */
class Ad extends CActiveRecord
{
	const STATUS_PENDING=0;
	const STATUS_ACTIVE=1;
	const STATUS_PAUSED=2;
	const STATUS_STOPPED=3;
	const STATUS_REMOVED=4;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Ad the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{ad}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('uid, details', 'required'),
			array('uid, status, budget', 'numerical', 'integerOnly'=>true),
			array('date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, uid, status, date, budget, details', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'count' => array(self::HAS_MANY, 'Adcount', 'adid','select'=>'uv,click'),
			'uvCount'=>array(self::STAT,'Adcount','adid','select'=>'sum(uv)'),
			'clickCount'=>array(self::STAT,'Adcount','adid','select'=>'sum(click)'),
			'impCount'=>array(self::STAT,'Adcount','adid','select'=>'sum(imp)'),
			'spentCount'=>array(self::STAT,'Adcount','adid','select'=>'sum(spent)'),
			'sloganCount'=>array(self::STAT,'Slogan','adid'),
			'publishedCount'=>array(self::STAT,'Published','adid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'uid' => 'Uid',
			'status' => 'Status',
			'date' => 'Date',
			'slogans' => 'Slogans',
			'published' => 'Published',
			'budget' => 'Budget',
			'details' => 'Details',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id);
		$criteria->compare('uid',$this->uid);
		$criteria->compare('status',$this->status);
		$criteria->compare('date',$this->date,true);
		$criteria->compare('budget',$this->budget);
		$criteria->compare('details',$this->details,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function afterFind(){
		$adsForm = new AdsForm;
		$this->details = $adsForm->search($this->details);
		parent::afterFind();
	}
	
	public function beforeSave(){
		$this->details = json_encode($this->details->attributes);
		return parent::beforeSave();
	}
	
	public function beforeValidate(){
		if($this->isNewRecord){
			$this->uid = Yii::app()->user->id;
			$this->date = date('Y-m-d H:i:s');
		}
		return parent::beforeValidate();
	}
	
	public static function converStatus($stat){
		if(is_numeric($stat)){
			switch($stat){
				case self::STATUS_ACTIVE : return 'active';break;
				case self::STATUS_PAUSED : return 'paused';break;
				case self::STATUS_REMOVED : return 'removed';break;
				case self::STATUS_STOPPED : return 'stopped';break;
				default : return 'pending';break;
			}
		}else{
			switch($stat){
				case 'active':return self::STATUS_ACTIVE;break;
				case 'paused':return self::STATUS_PAUSED;break;
				case 'removed':return self::STATUS_REMOVED;break;
				case 'stopped':return self::STATUS_STOPPED;break;
				default : return self::STATUS_PENDING;break;
			}
		}
		return false;
	}
}